const String tableDevices = 'devices';

class DeviceFields {
  static final List<String> values = [
    /// Add all fields
    id, name, nickName, deviceId, mac, mcuVersion, bleVersion, createTime,
    placeId, floorId, roomId, productId, catId, productKey, isOffline,
    serialNumber,
    clientId, host, deviceAccessKeyId, deviceAccessKeySecret, instanceId
  ];
  static const String id = '_id'; //数据库主键
  static const String deviceId = 'deviceId'; //服务器返回的设备Id
  static const String name = 'name';
  static const String nickName = 'nickName';
  static const String mac = 'mac'; //蓝牙mac唯一标识
  static const String mcuVersion = 'mcuVersion'; //mac版本
  static const String bleVersion = 'bleVersion'; //蓝牙版本
  static const String createTime = 'createTime'; //app创建时间
  static const String placeId = 'placeId'; //场所ID
  static const String floorId = 'floorId'; //楼层ID
  static const String roomId = 'roomId'; //房间ID
  static const String productId = 'productId';
  static const String catId = 'catId'; //分类
  static const String productKey = 'productKey'; //产品类型
  static const String isOffline = 'isOffline'; //在离线
  static const String serialNumber = 'serialNumber'; //系列号
  static const String clientId = 'clientId'; //mqtt相关
  static const String host = 'host'; //mqtt相关
  static const String deviceAccessKeyId = 'deviceAccessKeyId'; //mqtt相关
  static const String deviceAccessKeySecret = 'deviceAccessKeySecret'; //mqtt相关
  static const String instanceId = 'instanceId'; //mqtt相关
  static const String ip = 'ip'; //id地址
  static const String subnetMask = 'subnetMask'; //子网掩码
  static const String gatewayAddress = 'gatewayAddress'; //网关
  static const String dhcp = 'dhcp'; //是否开启自动分配ip
}

class Device {
  final int? id;
  final String deviceId;
  final String name;
  final String nickName;
  final String mac;
  final String mcuVersion;
  final String bleVersion;
  final String createTime;
  final String placeId;
  final String floorId;
  final String roomId;
  final int productId;
  final int catId;
  final int productKey;
  final bool isOffline;
  final String serialNumber;
  final String clientId;
  final String host;
  final String deviceAccessKeyId;
  final String deviceAccessKeySecret;
  final String instanceId;
  final String ip;
  final String subnetMask;
  final String gatewayAddress;
  final bool dhcp;

  @override
  String toString() {
    return 'Device{id: $id, deviceId: $deviceId, name: $name, nickName: $nickName, mac: $mac, mcuVersion: $mcuVersion, bleVersion: $bleVersion, createTime: $createTime, placeId: $placeId, floorId: $floorId, roomId: $roomId, productId: $productId, catId: $catId, productKey: $productKey, isOffline: $isOffline, serialNumber: $serialNumber, clientId: $clientId, host: $host, deviceAccessKeyId: $deviceAccessKeyId, deviceAccessKeySecret: $deviceAccessKeySecret, instanceId: $instanceId}';
  }

  const Device({
    this.id,
    required this.deviceId,
    required this.name,
    required this.nickName,
    required this.mac,
    required this.mcuVersion,
    required this.bleVersion,
    required this.createTime,
    required this.placeId,
    required this.floorId,
    required this.roomId,
    required this.productId,
    required this.catId,
    required this.productKey,
    required this.isOffline,
    required this.serialNumber,
    required this.clientId,
    required this.host,
    required this.deviceAccessKeyId,
    required this.deviceAccessKeySecret,
    required this.instanceId,
    required this.ip,
    required this.subnetMask,
    required this.gatewayAddress,
    required this.dhcp,
  });

  Device copy({
    int? id,
    String? deviceId,
    String? name,
    String? nickName,
    String? mac,
    String? mcuVersion,
    String? bleVersion,
    String? createTime,
    String? placeId,
    String? floorId,
    String? roomId,
    int? productId,
    int? catId,
    int? productKey,
    bool? isOffline,
    String? serialNumber,
    String? clientId,
    String? host,
    String? deviceAccessKeyId,
    String? deviceAccessKeySecret,
    String? instanceId,
    String? ip,
    String? subnetMask,
    String? gatewayAddress,
    bool? dhcp,
  }) =>
      Device(
          id: id ?? this.id,
          deviceId: deviceId ?? this.deviceId,
          name: name ?? this.name,
          nickName: nickName ?? this.nickName,
          mac: mac ?? this.mac,
          mcuVersion: mcuVersion ?? this.mcuVersion,
          bleVersion: bleVersion ?? this.bleVersion,
          createTime: createTime ?? this.createTime,
          placeId: placeId ?? this.placeId,
          floorId: floorId ?? this.floorId,
          roomId: roomId ?? this.roomId,
          productId: productId ?? this.productId,
          catId: catId ?? this.catId,
          productKey: productKey ?? this.productKey,
          isOffline: isOffline ?? this.isOffline,
          serialNumber: serialNumber ?? this.serialNumber,
          clientId: clientId ?? this.clientId,
          host: host ?? this.host,
          deviceAccessKeyId: deviceAccessKeyId ?? this.deviceAccessKeyId,
          deviceAccessKeySecret:
              deviceAccessKeySecret ?? this.deviceAccessKeySecret,
          instanceId: instanceId ?? this.instanceId,
          ip: ip ?? this.ip,
          subnetMask: subnetMask ?? this.subnetMask,
          gatewayAddress: gatewayAddress ?? this.gatewayAddress,
          dhcp: dhcp ?? this.dhcp);

  static Device fromJson(Map<String, Object?> json) => Device(
        id: json[DeviceFields.id] as int?,
        deviceId: json[DeviceFields.deviceId] as String,
        name: json[DeviceFields.name] as String,
        nickName: json[DeviceFields.nickName] as String,
        mac: json[DeviceFields.mac] as String,
        mcuVersion: json[DeviceFields.mcuVersion] as String,
        bleVersion: json[DeviceFields.bleVersion] as String,
        createTime: json[DeviceFields.createTime] as String,
        placeId: json[DeviceFields.placeId] as String,
        floorId: json[DeviceFields.floorId] as String,
        roomId: json[DeviceFields.roomId] as String,
        productId: json[DeviceFields.productId] as int,
        catId: json[DeviceFields.catId] as int,
        productKey: json[DeviceFields.productKey] as int,
        isOffline: json[DeviceFields.isOffline] as bool,
        serialNumber: json[DeviceFields.serialNumber] as String,
        clientId: json[DeviceFields.clientId] as String,
        host: json[DeviceFields.host] as String,
        deviceAccessKeyId: json[DeviceFields.deviceAccessKeyId] as String,
        deviceAccessKeySecret:
            json[DeviceFields.deviceAccessKeySecret] as String,
        instanceId: json[DeviceFields.instanceId] as String,
        ip: json[DeviceFields.ip] as String,
        subnetMask: json[DeviceFields.subnetMask] as String,
        gatewayAddress: json[DeviceFields.gatewayAddress] as String,
        dhcp: json[DeviceFields.dhcp] as bool,
      );

  Map<String, Object?> toJson() => {
        DeviceFields.id: id,
        DeviceFields.deviceId: deviceId,
        DeviceFields.name: name,
        DeviceFields.nickName: nickName,
        DeviceFields.mac: mac,
        DeviceFields.mcuVersion: mcuVersion,
        DeviceFields.bleVersion: bleVersion,
        DeviceFields.createTime: createTime,
        DeviceFields.placeId: placeId,
        DeviceFields.floorId: floorId,
        DeviceFields.roomId: roomId,
        DeviceFields.productId: productId,
        DeviceFields.catId: catId,
        DeviceFields.productKey: productKey,
        DeviceFields.isOffline: isOffline,
        DeviceFields.serialNumber: serialNumber,
        DeviceFields.clientId: clientId,
        DeviceFields.host: host,
        DeviceFields.deviceAccessKeyId: deviceAccessKeyId,
        DeviceFields.deviceAccessKeySecret: deviceAccessKeySecret,
        DeviceFields.deviceAccessKeySecret: deviceAccessKeySecret,
        DeviceFields.instanceId: instanceId,
        DeviceFields.ip: ip,
        DeviceFields.subnetMask: subnetMask,
        DeviceFields.gatewayAddress: gatewayAddress,
        DeviceFields.dhcp: dhcp,
      };
}
